//
//  AppDelegate.swift
//  SinaWeChat
//
//  Created by mac on 15/12/5.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    
    var observer: NSObjectProtocol?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        
        window  = UIWindow(frame:UIScreen.mainScreen().bounds)
        
        
        //更改背景颜色
        window?.rootViewController = GNUserAccountViewModel.sharedAccount.userLogon ? defaultRootViewController() : GNMainController()
        
        //成为主窗口并显示
        window?.makeKeyAndVisible()
        
        
        observer = NSNotificationCenter.defaultCenter().addObserverForName(GNSwitchRootVCNotifation, object: nil, queue: nil){[weak self] (noti) -> Void in
            print("切换根控制器")
            if noti.object is GNOAuthViewController {
                self?.window?.rootViewController = GNWelcomeViewController()
            }else{
                self?.window?.rootViewController = GNMainController()
            }
        }
        
        return true
    }
    
    //看进入新特性和欢迎页
    
    private func defaultRootViewController() -> UIViewController{
        
        
        if hasNewVersion() {
            return GNNewfeatureViewController()
        }else{
            return GNWelcomeViewController()
        }
    }
    
    
    //是否有新版本
    private func hasNewVersion() -> Bool {
        
        let versionKey = "versonKey"
        
        //取出当前 app 的版本号
        let localVersion = NSUserDefaults.standardUserDefaults().stringForKey(versionKey)
        
        //取出当前 app 的版本号
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"]! as! String
        
        //把当前的版本号保存到本地
        NSUserDefaults.standardUserDefaults().setObject(currentVersion, forKey: versionKey)
        
        // currentVersion 比 local 大
        // "a" "b" --> 自然排序
        
        // 以后开发中比较版本号尽量使用字符串
        // app的版本号不要超过2个小数点 不能使用(1.1.1.1)
        // 比较的时候,尽是不要比较字符串是否一样.
        //1.1.2是比1.1.10大的
        //另外注意是否在不同平台版本不同
        
        if let local = localVersion where
            currentVersion.compare(local) != NSComparisonResult.OrderedDescending{
                return false
        }
        
        print("\(localVersion),\(currentVersion)")
        //进行对比
        return true
        
    }
    
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(observer!)
    }

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

